home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / MakeIcon / TN120_Gworld.c < prev   
Encoding:
C/C++ Source or Header  |  1992-07-15  |  5.5 KB  |  155 lines  |  [TEXT/KAHL]

  1. #include <QDOffscreen.h>
  2.  
  3. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4.  
  5.     THE CODE BELOW HERE WAS TAKEN STRAIGHT FROM TECH NOTE #120.
  6.     SEE THAT TECH NOTE FOR MORE INFORMATION.  (I did remove one parameter from set up pix map.)
  7.   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  8.  
  9. #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
  10.  
  11. OSErr SetUpPixMap(
  12.     short        depth,       /* Desired number of bits/pixel in off-screen */
  13.     Rect         *bounds,     /* Bounding rectangle of off-screen */
  14.     CTabHandle   colors,      /* Color table to assign to off-screen */
  15.     PixMapHandle aPixMap      /* Handle to the PixMap being initialized */
  16.     )
  17.     {
  18.     CTabHandle newColors;       /* Color table used for the off-screen PixMap */
  19.     Ptr        offBaseAddr;     /* Pointer to the off-screen pixel image */
  20.     OSErr      error;           /* Returns error code */
  21.     short      bytesPerRow;        /* Number of bytes per row in the PixMap */
  22.  
  23.  
  24.     error = noErr;
  25.     newColors = nil;
  26.     offBaseAddr = nil;
  27.  
  28.       bytesPerRow = ((depth * (bounds->right - bounds->left) + 31) / 32) * 4;
  29.  
  30.    /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
  31.     if (depth <= 8)
  32.         {
  33.         newColors = colors;
  34.         error = HandToHand((Handle *) &newColors);
  35.         }
  36.     else
  37.         {
  38.         newColors = (CTabHandle) NewHandle(sizeof(ColorTable) -
  39.                 sizeof(CSpecArray));
  40.         error = MemError();
  41.         }
  42.     if (error == noErr)
  43.         {
  44.         /* Allocate pixel image; long integer multiplication avoids overflow */
  45.         offBaseAddr = NewPtr((unsigned long) bytesPerRow * (bounds->bottom -
  46.                 bounds->top));
  47.         if (offBaseAddr != nil)
  48.             {
  49.             /* Initialize fields common to indexed and direct PixMaps */
  50.             (**aPixMap).baseAddr = offBaseAddr;  /* Point to image */
  51.             (**aPixMap).rowBytes = bytesPerRow | /* MSB set for PixMap */
  52.                     0x8000;
  53.             (**aPixMap).bounds = *bounds;        /* Use given bounds */
  54.             (**aPixMap).pmVersion = 0;           /* No special stuff */
  55.             (**aPixMap).packType = 0;            /* Default PICT pack */
  56.             (**aPixMap).packSize = 0;            /* Always zero in mem */
  57.             (**aPixMap).hRes = kDefaultRes;      /* 72 DPI default res */
  58.             (**aPixMap).vRes = kDefaultRes;      /* 72 DPI default res */
  59.             (**aPixMap).pixelSize = depth;       /* Set # bits/pixel */
  60.             (**aPixMap).planeBytes = 0;          /* Not used */
  61.             (**aPixMap).pmReserved = 0;          /* Not used */
  62.  
  63.             /* Initialize fields specific to indexed and direct PixMaps */
  64.             if (depth <= 8)
  65.                 {
  66.                 /* PixMap is indexed */
  67.                 (**aPixMap).pixelType = 0;       /* Indicates indexed */
  68.                 (**aPixMap).cmpCount = 1;        /* Have 1 component */
  69.                 (**aPixMap).cmpSize = depth;     /* Component size=depth */
  70.                 (**aPixMap).pmTable = newColors; /* Handle to CLUT */
  71.                 }
  72.             else
  73.                 {
  74.                 /* PixMap is direct */
  75.                 (**aPixMap).pixelType = RGBDirect; /* Indicates direct */
  76.                 (**aPixMap).cmpCount = 3;          /* Have 3 components */
  77.                 if (depth == 16)
  78.                     (**aPixMap).cmpSize = 5;       /* 5 bits/component */
  79.                 else
  80.                     (**aPixMap).cmpSize = 8;       /* 8 bits/component */
  81.                 (**newColors).ctSeed = 3 * (**aPixMap).cmpSize;
  82.                 (**newColors).ctFlags = 0;
  83.                 (**newColors).ctSize = 0;
  84.                 (**aPixMap).pmTable = newColors;
  85.                 }
  86.             }
  87.         else
  88.             error = MemError();
  89.         }
  90.     else
  91.         newColors = nil;
  92.  
  93.     /* If no errors occured, return a handle to the new off-screen PixMap */
  94.     if (error != noErr)
  95.         {
  96.         if (newColors != nil)
  97.             DisposCTable(newColors);
  98.         }
  99.  
  100.     /* Return the error code */
  101.     return error;
  102.     }
  103.  
  104.  
  105. GWorldPtr CreateOffScreen (WindowPtr basePort, short depth, Boolean UseTempMem)
  106. {
  107.     CGrafPtr    currPort;    /* Pointer to the saved port */
  108.     GDHandle    currGDevice; /* Handle to the current GDevice */
  109.     GWorldPtr   offScreen;   /* Pointer to our GWorld */
  110.     Rect        offRect;     /* portRect of basePort in global coordinates */
  111.     GWorldFlags tempFlags;   /* Flag indicating if temp memory is to be used */
  112.     QDErr       result;      /* Error return from NewGWorld */
  113.  
  114.     GetGWorld (&currPort, &currGDevice);
  115.     SetPort (basePort);
  116.  
  117.     /* Globalize portRect of basePort */
  118.     offRect = basePort->portRect;
  119.     if (depth == 0)
  120.     {
  121.         LocalToGlobal (/*◊*/&topLeft(offRect));
  122.         LocalToGlobal (/*◊*/&botRight(offRect));
  123.     }
  124.  
  125.     /* Set tempFlags to reflect desire for temporary memory */
  126.     if (UseTempMem)
  127.         tempFlags = useTempMem;
  128.     else
  129.         tempFlags = 0;
  130.  
  131.     /* Create the new off-screen port and set it as the current port */
  132.     result = NewGWorld (/*<*/&offScreen, depth, &offRect, nil, nil, tempFlags);
  133.     if (result == noErr)
  134.     {
  135.         SetGWorld (offScreen, (GDHandle) nil);
  136.  
  137.         /* Good idea to set the clip region to the portRect of the new GWorld */
  138.         ClipRect (&offScreen->portRect);
  139.  
  140.         /* Clear the new GWorld to white */
  141.         if (LockPixels (GetGWorldPixMap (offScreen)))
  142.             {
  143.             EraseRect (&offScreen->portRect);
  144.             UnlockPixels (GetGWorldPixMap (offScreen));
  145.             }
  146.         }
  147.     else
  148.         offScreen = nil;
  149.  
  150.     /* Reset current GrafPort and GDevice to what it was when we were called */
  151.     SetGWorld (currPort, currGDevice);
  152.     return offScreen;
  153. }
  154.  
  155.